package com.apartment.own.controller;


import com.qiniu.util.Auth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.UUID;

/**
 * @program: HousingRental_tenant_Server_JAVA
 * @description: 七牛云
 * @author: @Dog_E
 * @create: 2020-09-15 13:38
 **/
@Controller
public class QnyController {

    private static ResourceBundle bundle = ResourceBundle.getBundle("qiniuyun");

    // ******的内容需要查看七牛云账号的相关信息
    /*private static final String accessKey = "Oy63b47SsWt-2eeuV4PJhpL0KS";    //访问秘钥
    private static final String secretKey = "dov6bhSYXXMGP0sxy7dZYBeRUKbIsPVa";    //授权秘钥
    private static final String bucket = "pinsgle";       //存储空间名称
    private static final String domain = "http://qiniu.huarnair.com/";       //外链域名*/



    /**
     * @Description: 给前端生成token
     * @param suffix: 文件后缀
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @Author: @Dog_E
     * @Date: 2020/9/15 0015 14:00
     **/
    @RequestMapping("/QiniuUpToken")
    @ResponseBody
    @CrossOrigin(value = "*")
    public Map<String, Object> QiniuUpToken(@RequestParam String suffix) throws Exception{
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            //验证七牛云身份是否通过
            Auth auth = Auth.create(bundle.getString("ACCESS_KEY"), bundle.getString("SECRET_KEY"));
            //生成凭证
            String upToken = auth.uploadToken(bundle.getString("bucketname"));
            result.put("token", upToken);
            //存入外链默认域名，用于拼接完整的资源外链路径
            result.put("domain", bundle.getString("DOMAIN"));
            //生成实际路径名
            String randomFileName = UUID.randomUUID().toString() + suffix;
            result.put("imgUrl", randomFileName);
            result.put("success", 1);
        } catch (Exception e) {
            result.put("message", "获取凭证失败，"+e.getMessage());
            result.put("success", 0);
        } finally {
            return result;
        }
    }
}
